#include"process.h"
#include<unistd.h>
#include<string.h>

#define LENGTH 101
#define LABLE '#'

//1、方法一
void process()
{
  //光标
  const char* sym = "|/-\\";
  int len = strlen(sym);
  //进度条
  char bar[LENGTH];
  //缓冲区
  memset(bar,'\0',sizeof(bar));
  int count = 0;
  
  while(count<=100)
  {
    printf("[%-100s][%d%%][%c]\r",bar,count,sym[count%len]);
    fflush(stdout);
    bar[count++] = LABLE;
    usleep(10000);
  }
  printf("\n");
}

//方法二
void FlushProcess(double target, double current)
{
  //定义光标
  const char* sym = "|/-\\";
  static int index = 0;
  int len = strlen(sym);

  //进度更新情况
  double rate = (current / target)*100.0;
  int count = (int)rate;
  //缓冲区
  char bar[LENGTH];
  memset(bar, 0, sizeof(bar));
  //插入#
  for(int i=0;i<count;++i)
  {
    bar[i]=LABLE;
  }
  //刷新
  printf("[%-100s][%.1lf%%][%c]\r",bar,rate,sym[index++]);
  index %= len;
  fflush(stdout);

  //当更新到100时就停止
  if(rate >= 100.0)
  {
    printf("\n");
  }
}
